package com.tre.jdevtemplateboot.web.filter;

import com.tre.jdevtemplateboot.common.util.TraceIdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author JDev
 */
public class LogBackIdWorkerFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(LogBackIdWorkerFilter.class);

    @Override
    public void init(FilterConfig filterConfig){
        logger.info("JDev LogBackIdWorkerFilter init starting.......。");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        TraceIdUtil.initTrace();
        filterChain.doFilter(request, response);
        TraceIdUtil.clearTrace();
    }

    @Override
    public void destroy(){
        logger.info("JDev LogBackIdWorkerFilter init destroyed.......。");
    }

}